package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonStudentSubjectiveDataBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.ui.activity.EveryDayWorkActivity;
import com.fancy.learncenter.utils.FileUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.fancy.learncenter.common.JumpIntentKey.QUSETION_BANK_LIST_KEY;

/**
 * A simple {@link Fragment} subclass.
 */
public class CartoonExpandVideoFragment extends BaseFragment {
    public static String EXPAND_VIDEO_KEY = "EXPAND_VIDEO_KEY";
    String playUrl = "";
    String coverImg = "";

    View rootView;
    @Bind(R.id.video_player)
    StandardGSYVideoPlayer videoPlayer;

    List<CartoonStudentSubjectiveDataBean.StemsListBean> stemsListBean;

    public CartoonExpandVideoFragment() {

    }
//    public CartoonExpandVideoFragment(List<CartoonStudentSubjectiveDataBean.StemsListBean> stemsListBean) {
//        this.stemsListBean = stemsListBean;
//        // Required empty public constructor
//
//        for (int i = 0; i < stemsListBean.size(); i++) {
//            switch (stemsListBean.get(i).getKey()) {
//                case "text":
//                    break;
//                case "video_url":
//                    playUrl = stemsListBean.get(i).getValue();
//                    break;
//                case "audio_url":
////                    playUrl = stemsListBean.get(i).getValue();
//                    break;
//                case "cover_img_url":
//                    coverImg = stemsListBean.get(i).getValue();
//                    break;
//            }
//        }
//    }

    public static CartoonExpandVideoFragment newInstance(ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean> stemsListBean) {

        Bundle args = new Bundle();
        args.putParcelableArrayList(QUSETION_BANK_LIST_KEY, stemsListBean);

        CartoonExpandVideoFragment fragment = new CartoonExpandVideoFragment();
        fragment.setArguments(args);

        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {


            this.stemsListBean = getArguments().getParcelableArrayList(QUSETION_BANK_LIST_KEY);
            // Required empty public constructor

            for (int i = 0; i < stemsListBean.size(); i++) {
                switch (stemsListBean.get(i).getKey()) {
                    case "text":
                        break;
                    case "video_url":
                        playUrl = stemsListBean.get(i).getValue();
                        break;
                    case "audio_url":
//                    playUrl = stemsListBean.get(i).getValue();
                        break;
                    case "cover_img_url":
                        coverImg = stemsListBean.get(i).getValue();
                        break;
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_cartoon_expand_video, container, false);

        ButterKnife.bind(this, rootView);

        initVideo(playUrl);
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();

        if (videoPlayer != null) {
            videoPlayer.onVideoPause();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        try {
            if (videoPlayer != null) {
                videoPlayer.removeAllViews();
                videoPlayer.release();
            }
        } catch (Exception e) {

        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (!isVisibleToUser) {
            if (videoPlayer != null) {
                videoPlayer.onVideoPause();
            }
        }

        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        if (videoPlayer != null) {
            videoPlayer.removeAllViews();
            videoPlayer.release();
        }
    }

    private void initVideo(String urlPath) {

        //需要路径的
        videoPlayer.setUp(urlPath, true, new File(FileUtils.getCartoonPath()), "非凡学习");
//        //增加封面
        SimpleDraweeView imageView = new SimpleDraweeView(getActivity());
        imageView.setImageURI(coverImg);
        videoPlayer.setThumbImageView(imageView);

        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.GONE);

        videoPlayer.setBottomProgressBarDrawable(getResources().getDrawable(R.drawable.video_new_progress));

        videoPlayer.setDialogVolumeProgressBar(getResources().getDrawable(R.drawable.video_new_volume_progress_bg));

        videoPlayer.setDialogProgressBar(getResources().getDrawable(R.drawable.video_new_progress));

        videoPlayer.setBottomShowProgressBarDrawable(getResources().getDrawable(R.drawable.video_new_seekbar_progress),
                getResources().getDrawable(R.drawable.video_new_seekbar_thumb));

        videoPlayer.setDialogProgressColor(getResources().getColor(R.color.yellow), getResources().getColor(R.color.white));

        videoPlayer.getFullscreenButton().setVisibility(View.GONE);
        videoPlayer.setDismissControlTime(500);
        videoPlayer.onPrepared();
    }

    @Override
    public String fmCallBackValue() {
        return null;
    }

    @Override
    public String fmCallBackName() {
        return null;
    }

    @Override
    public void fmCallBack() {

    }
}
